#!/usr/bin/env python3

import os

import common
from shell_helpers import LF

class Main(common.LkmcCliFunction):
    def __init__(self):
        super().__init__(
            defaults = {
                'show_time': False,
            },
            description='''Run a Buildroot ToolChain tool like readelf or objdump.

For example, to get some information about the arm vmlinux:

....
./%(prog)s readelf -- -e "$(./getvar vmlinux)"
....

Get the list of available tools with:

....
ls "$(./getvar -a arm buildroot_host_bin_dir)"
....
''',
        )
        self.add_argument(
            '--print-tool',
            default=False,
            help='''
Just output print tool path to stdout but don't actually run it.
Suitable for programmatic consumption by other shell programs.
''',
        )
        self.add_argument('tool', help='Which tool to run.')
        self.add_argument(
            'extra_args',
            default=[],
            help='Extra arguments for the tool.',
            metavar='extra-args',
            nargs='*'
        )

    def timed_main(self):
        if self.env['baremetal'] is None:
            image = self.env['vmlinux']
        else:
            image = self.env['image']
        tool = self.get_toolchain_tool(self.env['tool'])
        if self.env['print_tool']:
            print(tool)
            return 0
        else:
            return self.sh.run_cmd(
                [tool, LF]
                + self.sh.add_newlines(self.env['extra_args']),
                cmd_file=os.path.join(self.env['run_dir'], 'run-toolchain.sh'),
            )

if __name__ == '__main__':
    Main().cli()
